<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Topsis_Model extends Model {

    public function __construct() {
        parent::__construct();
    }

    public function list_profile_topsis() {

        $sth = $this->db->prepare("
            SELECT
            *
            FROM
            topsis
            ");
        $sth->execute();
        $data = $sth->fetchAll();

        return $data;
    }

    public function add_profile_topsis($data) {

        $sth = $this->db->prepare("
            INSERT INTO
            topsis
            (profile_id,nama_profile)
            VALUES
            (:profile_id,:nama_profile)
            ");
        $sth->execute(array(
            ':profile_id' => $data['profile_id'],
            ':nama_profile' => $data['nama_profile']
        ));
    }

    public function delete_profile_topsis($profile_id) {
        
    }

    public function edit_profile_topsis($profile_id) {
        
    }

    public function do_topsis($profile_id) {
        
        $hasil_final[]= array();

        $sth = $this->db->prepare("SELECT kd_makul,weight FROM topsis_detail WHERE profile_id =:profile_id");

        $sth->execute(array(
            ':profile_id' => $profile_id
        ));

        $hasil = $sth->fetchAll();

//        $data = array();

        $jumlah_criteria = $sth->rowCount();    

        foreach ($hasil as $value) {

            $update_weight = $this->db->prepare("UPDATE data set w = :w WHERE kd_makul = :kd_makul");
            $update_weight->execute(array(
                ':kd_makul' => $value['kd_makul'],
                ':w' => $value['weight']
            ));
//
//            print_r($hasil2);
//            echo ('<br>');
//            print_r($value['kd_makul']);
//            echo ('<br>');
//            $jumlah_opsi = $sth2->rowCount();
        }

        $kd_makul_jumlah[] = array();

        foreach ($hasil as $value) {
            $sth4 = $this->db->prepare("Select kd_makul,sum(x2),w from data where kd_makul = :kd_makul");
            $sth4->execute(array(
                ':kd_makul' => $value['kd_makul']
            ));

            $result3 = $sth4->fetchAll();


            foreach ($result3 as $value) {
                $kd_makul_jumlah['kd_makul'] = $value['kd_makul'];
                $kd_makul_jumlah['jumlah'] = floatval($value['sum(x2)']);
                $kd_makul_jumlah['weight'] = floatval($value['w']);
            }

            foreach ($kd_makul_jumlah as $value) {
                $sth5 = $this->db->prepare("Select nrp,kd_makul,(x2/:jumlah)*:weight as rataan from data where kd_makul = :kd_makul");

                $sth5->execute(array(
                    ':kd_makul' => $kd_makul_jumlah['kd_makul'],
                    ':jumlah' => $kd_makul_jumlah['jumlah'],
                    ':weight' => $kd_makul_jumlah['weight']
                ));

                $result5 = $sth5->fetchAll();
            }

            foreach ($result5 as $value) {

                $proses ['nrp'] = $value['nrp'];
                $proses ['kd_makul'] = $value['kd_makul'];
                $proses ['nilai'] = floatval($value['rataan']);

                $sth6 = $this->db->prepare
                        ("UPDATE data 
                            set x3 = :x3 
                            where 
                            nrp = :nrp 
                            and 
                            kd_makul = :kd_makul");
//                print_r($proses);
//                echo ('<br>');
                $sth6->execute(array(
                    ':x3' => floatval($value['rataan']),
                    ':nrp' => $value['nrp'],
                    ':kd_makul' => $value['kd_makul']
                ));
//                print_r($proses);
//                echo ('<br>');
            }
        }

        foreach ($hasil as $value) {
            $sth7 = $this->db->prepare("select kd_makul,max(x3) from data where kd_makul = :kd_makul");

            $sth7->execute(array(
                ':kd_makul' => $value['kd_makul']
            ));

            $result7 = $sth7->fetchAll();
//
//            print_r($result7);
//            echo ('<br>');
            foreach ($result7 as $val) {
                $update_max = $this->db->prepare("UPDATE data SET max = :max where kd_makul = :kd_makul");

                $update_max->execute(array(
                    ':max' => floatval($val['max(x3)']),
                    ':kd_makul' => $val['kd_makul']
                ));
//                print_r($proses2);
//                echo ('<br>');
            }
        }
        foreach ($hasil as $value) {
            $sth8 = $this->db->prepare("select kd_makul,min(x3) from data where kd_makul = :kd_makul");

            $sth8->execute(array(
                ':kd_makul' => $value['kd_makul']
            ));
            $result8 = $sth8->fetchAll();

            foreach ($result8 as $val) {
                $update_min = $this->db->prepare("UPDATE data SET min = :min WHERE kd_makul = :kd_makul");

                $update_min->execute(array(
                    ':min' => floatval($val['min(x3)']),
                    ':kd_makul' => $val['kd_makul']
                ));
            }
        }

        foreach ($hasil as $value) {
            $sth9 = $this->db->prepare("select nrp,kd_makul,x3,max,min from data where kd_makul = :kd_makul");

            $sth9->execute(array(
                ':kd_makul' => $value['kd_makul']
            ));
            $result9 = $sth9->fetchAll();
//                     
            $proses3 = array();
            foreach ($result9 as $val) {
//                print_r($val['nrp']);
//                echo ('<br>');
//                $proses3['kd_makul'] = $value['kd_makul'];
//                $proses3['x3']  = floatval($val['x3']);
//                $proses3['max'] = floatval($val['max']);
//                $proses3['min'] = floatval($val['min']);

                $positif = (floatval($val['x3']) - floatval($val['max'])) * (floatval($val['x3']) - floatval($val['max']));
                $negatif = (floatval($val['x3']) - floatval($val['min'])) * (floatval($val['x3']) - floatval($val['min']));
//                $index = $negatif/($negatif + $positif);
                
                $positif_query = $this->db->prepare("UPDATE data SET positif = :positif WHERE kd_makul = :kd_makul AND nrp = :nrp");

                $positif_query->execute(array(
                    ':positif' => $positif,
                    ':kd_makul' => $val['kd_makul'],
                    ':nrp' => $val['nrp']
                ));

                $negatif_query = $this->db->prepare("UPDATE data SET negatif = :negatif WHERE kd_makul = :kd_makul AND nrp = :nrp");

                $negatif_query->execute(array(
                    ':negatif' => $negatif,
                    ':kd_makul' => $val['kd_makul'],
                    ':nrp' => $val['nrp']
                ));
//                $index_query = $this->db->prepare("UPDATE data SET index = :index WHERE kd_makul = :kd_makul AND nrp = :nrp");
//
//                $index_query->execute(array(
//                    ':index' => $index,
//                    ':kd_makul' => $val['kd_makul'],
//                    ':nrp' => $val['nrp']
//                ));
            }
        }


        foreach ($hasil as $value) {
            $distinct_nrp = $this->db->prepare("SELECT nrp from data where kd_makul = :kd_makul");

            $distinct_nrp->execute(array(
                ':kd_makul' => $value['kd_makul']
            ));

            $result_disc = $distinct_nrp->fetchAll();

            $data_nrp = array();
            foreach ($result_disc as $value) {
                $data_nrp[] = $value['nrp'];
            }
        }

        $dsc = array_unique($data_nrp);

        foreach ($dsc as $val) {
            $fin = $this->db->prepare("select nrp,sum(positif),sum(negatif) from data where nrp = :nrp");
//            print_r($value);
//            echo '<br>';
            $fin->execute(array(
                ':nrp' => $val
            ));
            
            $result10 = $fin->fetchAll();
            
//            print_r($index);
//            echo '<br>';
            $index = array();
            foreach ($result10 as $value) {
                $index['nrp'] = $value['nrp'];
                $index['positif'] = SQRT(floatval($value['sum(positif)']));
                $index['negatif'] = SQRT(floatval($value['sum(negatif)']));
                $index['final'] = $index['negatif']/($index['positif'] + $index['negatif']);
                
                
                
            }
           var_dump($index);
//           echo '<br>';
           
                  
        }
        
//        var_dump($hasil_final);
//        return $hasil_final;
    }

}

?>
